﻿using CanDichVu.FrameWork.Conrtron;
using CanDichVu.Service.Iservice;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CanDichVu.Db;
using CanDichVu.Db.Entity;

namespace CanDichVU.Areas.Admin.Controllers
{
    public class Ad_CateController : ContronllerBase
    {
        private readonly ICate _icate;
        
        public Ad_CateController(ICate icate)
        {
            this._icate = icate;
        }


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            ChuyenMuc entity = new ChuyenMuc();
            entity.ParentId = 0;
            entity.Id = 0;
            entity.Hot = false;
            entity.stt = 0;
            entity.NgayTao = DateTime.Now;

            return PartialView("_Add", entity);
        }

        public ActionResult Update(int id)
        {
            ChuyenMuc entity = _icate.GetById(id);
            return PartialView("_Update", entity);
        }

        public ActionResult Delete(int id)
        {
            try
            {
                _icate.detele(id);
                return JsonSuccess(Url.Action("Index"), null);
            }
            catch (Exception ex)
            {
                return JsonError(ex.ToString());
            }
        }

        public ActionResult _List()
        {
            List<ChuyenMuc> Model = new List<ChuyenMuc>();
            var categories = _icate.GetAllCategories();

            foreach (var item in categories)
            {
                item.TenChuyenMuc = GetCategoryBreadCrumbNone(item, _icate);
                Model.Add(item);
            }

            Model = Model.OrderBy(p => p.TenChuyenMuc).ToList();

            var jsonResult = Json(Model, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }

        public ActionResult getCateParent()
        {
            List<ChuyenMuc> categories = new List<ChuyenMuc>();
            categories = _icate.getListByParent(0);
            var jsonResult = Json(categories, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }

        public ActionResult AddOrUpdateCate(ChuyenMuc model)
        {
            if (string.IsNullOrEmpty(model.TenChuyenMuc))
            {
                return JsonError(" Vui lòng nhập tên chuyên mục ");
            }

            if (string.IsNullOrEmpty(model.Icon))
            {
                model.Icon = "/Content/images/IconNone.png";
            }

            if (model.Hot == null) model.Hot = false;
            if (model.set_footer == null) model.set_footer = false;

            _icate.addorUpdate(model);
            return JsonSuccess("/Admin/Ad_Cate/Index", null);
        }

        [HttpPost]
        public ActionResult UpdateMuitl(List<ChuyenMuc> lists)
        {

            foreach (var item in lists)
            {
                var entity = _icate.GetById(item.Id);
                entity.stt = item.stt;
                entity.Hot = item.Hot;
                _icate.addorUpdate(entity);
            }

            return RedirectToAction("Index");
        }

        public ActionResult AllCategories(int selectedId)
        {
            var categories = _icate.GetAllCategories().OrderBy( p => p.TenChuyenMuc.Substring(0,1)).ToList();
            categories.Insert(0, new ChuyenMuc { TenChuyenMuc = "[Tất cả các chuyên mục]", Id = 0 });
            var selectList = new List<SelectListItem>();
            foreach (var c in categories)
                selectList.Add(new SelectListItem()
                {
                    Value = c.Id.ToString(),
                    Text = GetCategoryBreadCrumb(c, _icate),
                    Selected = c.Id == selectedId
                });

            return new JsonResult { Data = selectList, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

        public ActionResult GetCateHoaDorpList()
        {
            var _listEntity = new List<ListEntityDorpList>();
            _listEntity.Add(new ListEntityDorpList { id = 0, text = "--Không có chuyên mục cha--", children = null });
            _listEntity.AddRange(_icate.ListCateAllDorp(0));
            var jsonResult = Json(_listEntity, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }

        

        public string GetCategoryBreadCrumb(ChuyenMuc category, ICate categoryService)
        {
            string result = string.Empty;

            try
            {
                if (category.Id == 0)
                {
                    result = "[None]";
                }
                else
                {

                    while (category != null)
                    {
                        if (String.IsNullOrEmpty(result))
                            result = category.TenChuyenMuc;
                        else
                            result = category.TenChuyenMuc + " =>> " + result;

                        category = categoryService.GetById(category.ParentId.Value);

                    }
                }
            }
            catch { result = "[None]"; }

            return result;
        }

        public string GetCategoryBreadCrumbNone(ChuyenMuc category, ICate categoryService)
        {
            string result = string.Empty;

            try
            {
                if (category.Id == 0)
                {
                    result = "[None]";
                }
                else
                {

                    while (category != null)
                    {
                        if (String.IsNullOrEmpty(result))
                            result = category.TenChuyenMuc;
                        else
                            result = category.TenChuyenMuc + " =>> " + result;

                        category = categoryService.GetById(category.ParentId.Value);

                    }
                }
            }
            catch { result = "[None]"; }

            return result;
        }

    }
}
